package com.wudimeng.syll.controller.api;


import com.wudimeng.syll.common.ApiResponse;
import com.wudimeng.syll.constants.Constant;
import com.wudimeng.syll.entity.CRuKuXinXi;
import com.wudimeng.syll.service.ICRuKuXinXiService;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author laohei
 * @Date 2020/6/8 9:50
 * @Version 1.0
 */
@RestController
@Slf4j
@RequestMapping("syll")
public class CRuKuXinXiController {

    @Autowired
    private ICRuKuXinXiService service;

    @GetMapping("/RuKuXinXi")
    public ApiResponse getRuKuXinXi(HttpServletRequest  request){
        try {
            Map<String,String> map = new HashMap<>();
            map.put("startDate",request.getParameter("startDate"));
            map.put("endDate",request.getParameter("endDate"));
            map.put("MingCheng",request.getParameter("MingCheng"));
            map.put("LengKu",request.getParameter("LengKu"));
            map.put("YongHuID",request.getParameter("YongHuID"));
            map.put("WeiTuoID",request.getParameter("WeiTuoID"));
            map.put("ShiFouJinKou",request.getParameter("ShiFouJinKou"));
            Integer pageNum = Integer.valueOf(request.getParameter("pageNum"));
            Integer pageSize = Integer.valueOf(request.getParameter("pageSize"));
            return this.service.selectCRuKuXinXi(map,pageNum,pageSize);
        } catch (Exception e) {
            log.error("CRuKuXinXiController.getRuKuXinXi.请稍后再试:" + e.getMessage());
            return ApiResponse.error(Constant.COMMON_PARAMETER_DEGRADE);
        }
    }

    @PostMapping("/RuKuXinXi")
    public ApiResponse addRuKuXinXi(@RequestBody  @Valid CRuKuXinXi ruKuXinXi, BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            return ApiResponse.error(bindingResult.getFieldError().getDefaultMessage());
        }
       return service.addCRuKuXinXi(ruKuXinXi);
    }


    @PutMapping("/RuKuXinXi")
    public ApiResponse updRuKuXinXi(@RequestBody CRuKuXinXi ruKuXinXi,HttpServletRequest request){
        return  service.updCRuKuXinXi(ruKuXinXi);
    }

    @DeleteMapping ("/RuKuXinXi")
    public ApiResponse delRKuXinXi(String liuShuiID, HttpServletRequest request){
        return service.delCRuKuXinXi(liuShuiID);
    }

    //入库信息
    @GetMapping("/getRuKuXinXiByLiuShuiHao")
    public ApiResponse getRuKuXinXiByLiuShuiHao(HttpServletRequest request){
        try {
            return service.selectCRuKuXinXiByLiuShuiHao(request.getParameter("liuShuiHao"),request.getParameter("liuShuiID"));
        } catch (Exception e) {
            e.printStackTrace();
            return ApiResponse.error(Constant.COMMON_OPERATE_FAIL);
        }
    }

    /**
     * 流水号查询【入库】信息  【入库】查不到使用该流水号查【出库】信息，返回信息中 type字段表示是出库还是入库
     * @param request
     * @return
     */
    @GetMapping("/getTypeByLiuShuiHao")
    public ApiResponse getTypeByLiuShuiHao(HttpServletRequest request){
        try {
            return service.getTypeByLiuShuiHao(request.getParameter("liuShuiHao"));
        } catch (Exception e) {
            e.printStackTrace();
            return ApiResponse.error(Constant.COMMON_OPERATE_FAIL);
        }
    }


    //查询入库流水号  用流水号模糊查询
    @GetMapping("/getRuKuLiuShuiHao")
    public ApiResponse getRuKuLiuShuiHao(HttpServletRequest request){
        try {
            String liuShuiHao = request.getParameter("liuShuiHao");
            String yongHuId = request.getParameter("yongHuID");
            String weiTuoId = request.getParameter("weiTuoID");
            Map<String,String> map = new HashMap<>();
            map.put("liuShuiHao",liuShuiHao);
            map.put("yongHuId",yongHuId);
            map.put("weiTuoId",weiTuoId);
            return service.getRuKuLiuShuiHao(map);
        } catch (Exception e) {
            e.printStackTrace();
            return ApiResponse.error(Constant.COMMON_OPERATE_FAIL);
        }
    }

    /**
     * 出库流水号追溯链条
     * @param request
     * @return
     */
    @GetMapping("/liuShuiHaoZhuiSu")
    public ApiResponse liuShuiHaoZhuiSu(HttpServletRequest request){
        try {
            return service.liuShuiHaoZhuiSu(request.getParameter("liuShuiHao"));
        } catch (Exception e) {
            e.printStackTrace();
            return ApiResponse.error(Constant.COMMON_OPERATE_FAIL);
        }
    }
}
